home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Hot Mix 17
/
Hot Mix 17.iso
/
HM17_SGI
/
research
/
examples
/
insight
/
plugins
/
mydat.pro
< prev
next >
Wrap
Text File
|
1997-07-08
|
29KB
|
850 lines
; $Id: mydat.pro,v 1.13 1997/03/28 17:08:14 billo Exp $
;
; Copyright (c) 1997, Research Systems, Inc. All rights reserved.
; Unauthorized reproduction prohibited.
;+
; FILE:
; mydat.pro
;
; PURPOSE:
; This file contains a File PlugIn that reads the .dat files
; in the examples directory of the IDL distribution. It can also
; be used to read other structured binary files.
;
; CONTENTS:
; CALLBACK ROUTINES
; pro WriteMydat - writes a structured binary file
; pro ReadMydat - reads a structured binary file
; dat_CheckDataDir - Checks for file in the example/data directory
; dat_BuildType - Builds data for the READU routine.
; dat_Verify - Verifys user input into the dialog.
; dat_ComputeFields - Computes the field values when others change
; dat_HandleEvents - Handles dialog events
; dat_PromptUser - Builds and displays the dialog
;
; REGISTRATION FUNCTION
; fun Mydat - registers the PlugIn
;
;-
; *****************************************************************************
; CALLBACK ROUTINES
; *****************************************************************************
; -----------------------------------------------------------------------------
;
; Purpose: Check to see if the file is in the EXAMPLE/DATA directory. If so,
; get the information about it needed to read it in.
;
function dat_CheckDataDir, $
filename, $ ; IN: name of file to read
fileTail, $ ; IN: name of file minus directory
DESCRIPTION=description, $ ; OUT: the description of the data read
DIMENSIONS=dimensions, $ ; OUT: the dimensions of the data read
CLASSIFICATION=classification, $; OUT: the classification of the data read
FIRST_DATA_ITEM=firstDataItem, $; OUT: the first element to read
LAST_DATA_ITEM=lastDataItem ; OUT: the last element to read.
name = ''
dims = LONARR(3)
desc = ''
delimiter = ''
numfiles = 0L
fileFound = 0B
; Look for the file, and set up the data array and data name.
;
OPENR, unit, FILEPATH("data.txt", SUBDIRECTORY=['examples', 'data']), $
/GET_LUN
READF, unit, numfiles
if (numfiles gt 0) then begin
for nameindex = 0, numfiles - 1 do begin
READF, unit, name, dims, desc, delimiter
if (name eq fileTail) then begin
fileFound = 1B
dimensions = dims
description = desc
firstDataItem = 0
lastDataItem = 0
dimIndexes = WHERE(dimensions gt 1, dimCount)
if (dimCount eq 1) then begin
classification = 'Vector'
dimensions[1:2] = 0
endif else begin
classification = 'Indexed Image'
if (dimCount ge 2) then begin
lastDataItem = dimensions[2]-1
dimensions[2] = 0
endif
endelse
goto, FILE_FOUND
endif
if (delimiter ne '*') then $
MESSAGE, "* delimiter not found in data.txt"
endfor
endif
FILE_FOUND:
FREE_LUN, unit
return, fileFound
end
; -----------------------------------------------------------------------------
;
; Purpose: Create the data to serve as the container for the READU routine.
;
function dat_BuildType, $
type, $ ; IN: IDL data type of data to create.
dimSizes ; IN: Vector of sizes for each dimension.
; RESULT: Data to serve as container for READU routine.
index = WHERE(dimSizes gt 0, nDims)
; Array Value
;
if (nDims gt 0) then begin
; Build up a size array for making the array
sizeArray = LONARR(3+nDims)
nElts = 1
for i=0, nDims-1 do $
nElts = dimSizes[i] * nElts
sizeArray[0] = nDims
sizeArray[1:nDims] = dimSizes[0:nDims-1]
sizeArray[nDims+1] = type
sizeArray[nDims+2] = nElts
value = MAKE_ARRAY(SIZE=sizeArray)
; Scalar
;
endif else begin
case (type) of
0: Value = 0 ;; Can't return undef but undef is OK.
1: Value = 0B
2: Value = 0
3: Value = 0L
4: Value = 0.0
5: Value = 0.0D
6: Value = COMPLEX(0)
7: Value = ''
9: Value = DCOMPLEX(0)
else:
endcase
endelse
RETURN, value
end
; -----------------------------------------------------------------------------
;
; Purpose: Verify what was entered into the dialog is correct.
;
function dat_Verify, $
psState ; IN: Pointer to structure of widget ids and values.
; RESULT: 1B for success. Will throw errors (unsuccessful).
; Get base name of data item.
;
WIDGET_CONTROL, (*psState).wNameText, GET_VALUE=dataName
(*psState).name = dataName[0]
; Get the description of data.
;
WIDGET_CONTROL, (*psState).wDescriptionText, GET_VALUE=description
(*psState).description = description[0]
; Get starting byte offset into the file.
;
WIDGET_CONTROL, (*psState).wOffsetText, GET_VALUE=offset
offset = LONG(offset[0])
if ((offset ge (*psState).fileSize) or (offset lt 0)) then $
MESSAGE,'The "offset" must be greater than or equal to zero and'+ $
' less than to the file size ('+$
STRING((*psState).fileSize, FORMAT='(I0)')+') in bytes.' $
else $
(*psState).offset = offset
; Get IDL data type.
;
dataTypeIndex = WIDGET_INFO((*psState).wTypeList, /DROPLIST_SELECT)
(*psState).dataTypeIndex = dataTypeIndex
; Get the classification.
;
classIndex = WIDGET_INFO((*psState).wClassList, /DROPLIST_SELECT)
(*psState).classIndex = classIndex
; Get starting data element.
;
WIDGET_CONTROL, (*psState).wFirstDataItemText, GET_VALUE=first
(*psState).firstDataItem = LONG(first[0])
; Get ending data element.
;
WIDGET_CONTROL, (*psState).wLastDataItemText, GET_VALUE=last
(*psState).lastDataItem = LONG(last[0])
if ((*psState).firstDataItem gt (*psState).lastDataItem) then $
MESSAGE,'The "From" index must be less than or equal to the "To" index'
; Get dimension sizes.
;
for seqDim = 0, 7 do begin
WIDGET_CONTROL, (*psState).wSizeText[seqDim], GET_VALUE=dimSize
(*psState).dimensions[seqDim] = LONG(dimSize[0])
endfor
; Create the empty data (actually a pointer to it) to READU into.
;
(*psState).pData = $
PTR_NEW(dat_BuildType((*psState).dataTypes[dataTypeIndex], $
(*psState).dimensions))
; If reached this point, return success
;
RETURN, 1B
end
; -----------------------------------------------------------------------------
;
; Purpose: Return the number of bytes for the IDL data type.
;
function dat_SizeOf, type
; Get the number of bytes per element.
;
case (type) of
1: nBytes = 1L ; Byte
2: nBytes = 2L ; Int
3: nBytes = 4L ; Long
4: nBytes = 4L ; Float
5: nBytes = 8L ; Double
6: nBytes = 8L ; Complex
9: nBytes = 16L ; Double Complex
else: MESSAGE, 'Unsupported IDL data type.'
endcase
return, nBytes
end
; -----------------------------------------------------------------------------
;
; Purpose: Recompute field values when something changes in the dialog.
;
pro dat_ComputeFields, psState, $
SET_FROM_TO=setFromTo, $ ; IN: true if need to compute From and To fields.
SET_LEFTOVER=setLeftOver ; IN: true if only need to compute LeftOver field.
if (dat_Verify(psState)) then begin
; Get the number of bytes per element.
;
nBytes = dat_SizeOf((*psState).dataTypes[(*psState).dataTypeIndex])
; Compute the file size based on the starting offset into the file.
;
newSize = (*psState).fileSize - (*psState).offset
; Compute the number of elements in the file based on the size of
; the data type.
;
numElements = newSize / nBytes
; Take the root to guess at square dimensions
;
numDims = (*psState).numDimensions[(*psState).classIndex]
; Deal with true color image formats specially.
;
case (*psState).classNames[(*psState).classIndex] of
'Pixel Interleaved Image': begin
numElements = numElements / 3
dims = numElements ^ (1./2)
dimensions = [3, dims, dims, 0,0,0,0,0]
end
'Line Interleaved Image': begin
numElements = numElements / 3
dims = numElements ^ (1./2)
dimensions = [dims, 3, dims, 0,0,0,0,0]
end
'Image Interleaved Image': begin
numElements = numElements / 3
dims = numElements ^ (1./2)
dimensions = [dims, dims, 3, 0,0,0,0,0]
end
else: begin
dims = numElements ^ (1./numDims)
dimensions = LONARR(8)
if (numDims gt 0) then $
dimensions[0:numDims-1] = dims
end
endcase
; The From and To fields need to be computed when the user
; changes the values in the dimensions fields.
;
if (KEYWORD_SET(setFromTo)) then begin
elementsPerData = 1
for seqDim = 0, numDims-1 do begin
if ((*psState).dimensions[seqDim] lt 0) then $
MESSAGE,'Dimensions must be greater than zero.' $
else $
elementsPerData = $
(*psState).dimensions[seqDim] * elementsPerData
endfor
(*psState).firstDataItem = 0
(*psState).lastDataItem = LONG(numElements/elementsPerData)
if ((*psState).lastDataItem gt 0) then $
(*psState).lastDataItem = (*psState).lastDataItem - 1
WIDGET_CONTROL, (*psState).wFirstDataItemText, SET_VALUE='0'
WIDGET_CONTROL, (*psState).wLastDataItemText, $
SET_VALUE=STRING((*psState).lastDataItem, FORMAT='(I0)')
leftover = newSize - $
((((*psState).lastDataItem - (*psState).firstDataItem + 1) * $
elementsPerData) * nBytes)
endif else if (KEYWORD_SET(setLeftOver)) then begin
; Only need to compute the leftover bytes when the user edits
; the From or To fields.
;
elementsPerData = 1
for seqDim = 0, numDims-1 do begin
elementsPerData = $
(*psState).dimensions[seqDim] * elementsPerData
endfor
leftover = newSize - $
((((*psState).lastDataItem - (*psState).firstDataItem + 1) * $
elementsPerData) * nBytes)
endif else begin
; Compute the dimensions if the offset, data type or classification
; fields are changed by the user.
;
elementsPerData = 1
for seqDim = 0, 7 do $
if (seqDim lt numDims) then begin
WIDGET_CONTROL, (*psState).wSizeText[seqDim], $
SET_VALUE=STRING(dimensions[seqDim], FORMAT='(I0)'), $
/SENSITIVE
elementsPerData = dimensions[seqDim] * elementsPerData
endif else $
WIDGET_CONTROL, (*psState).wSizeText[seqDim], $
SET_VALUE='', SENSITIVE=0
WIDGET_CONTROL, (*psState).wFirstDataItemText, SET_VALUE='0'
WIDGET_CONTROL, (*psState).wLastDataItemText, SET_VALUE='0'
leftover = newSize - (elementsPerData * nBytes)
endelse
; Specify what classification of data the user is setting the
; From and To fields for.
;
WIDGET_CONTROL, (*psState).wClassLabel, $
SET_VALUE='('+(*psState).classNames[(*psState).classIndex]+'s)'
; If the fields represent an exact fit with the file, display nothing.
;
if (leftover eq 0) then $
WIDGET_CONTROL, (*psState).wLeftOverLabel, SET_VALUE='' $
else if (leftover gt 0) then $
; If the user has specified a format the comes up short of fitting
; the file, indicate how many bytes short that they are.
;
WIDGET_CONTROL, (*psState).wLeftOverLabel, SET_VALUE='( '+$
STRING(leftover, FORMAT='(I0)') + ' bytes short of EOF)' $
else $
; If the user has specified a format the goes past the end-of-file,
; indicate how many bytes past the EOF that they are.
;
WIDGET_CONTROL, (*psState).wLeftOverLabel, SET_VALUE='( '+$
STRING((-1)*leftover, FORMAT='(I0)') + ' bytes past EOF)'
endif
end
; -----------------------------------------------------------------------------
;
; Purpose: Handle events for the dialog.
;
pro dat_HandleEvents, sEvent
; Grab the state pointer from the widget user value.
;
WIDGET_CONTROL, sEvent.top, GET_UVALUE=psState
; Catch errors and display them in a dialog.
;
CATCH, error
if (error ne 0) then begin
CATCH, /CANCEL
void = DIALOG_MESSAGE(!ERR_STRING, DIALOG_PARENT=(*psState).wMainBase)
RETURN
endif
case (sEvent.id) of
(*psState).wOKButton: $
if (dat_Verify(psState)) then begin
; Set and return success flag.
;
(*psState).update = 1
WIDGET_CONTROL, sEvent.top, /DESTROY
endif
(*psState).wCancelButton: WIDGET_CONTROL, sEvent.top, /DESTROY
(*psState).wSizeText[0]: dat_ComputeFields, psState, /SET_FROM_TO
(*psState).wSizeText[1]: dat_ComputeFields, psState, /SET_FROM_TO
(*psState).wSizeText[2]: dat_ComputeFields, psState, /SET_FROM_TO
(*psState).wSizeText[3]: dat_ComputeFields, psState, /SET_FROM_TO
(*psState).wSizeText[4]: dat_ComputeFields, psState, /SET_FROM_TO
(*psState).wSizeText[5]: dat_ComputeFields, psState, /SET_FROM_TO
(*psState).wSizeText[6]: dat_ComputeFields, psState, /SET_FROM_TO
(*psState).wSizeText[7]: dat_ComputeFields, psState, /SET_FROM_TO
(*psState).wFirstDataItemText: dat_ComputeFields, psState, $
/SET_LEFTOVER
(*psState).wLastDataItemText: dat_ComputeFields, psState, $
/SET_LEFTOVER
else: dat_ComputeFields, psState
endcase
end
; -----------------------------------------------------------------------------
;
; Purpose: Create and display the dialog.
;
function dat_PromptUser, $
fileName, $ ; IN: Full file name to read in
fileSize, $ ; IN: Size in bytes of the file
DESCRIPTION=description, $ ; IN/OUT: Description of data
DIMENSIONS=dimensions, $ ; IN/OUT: Dimensions (from example/data)
OFFSET=offset, $ ; IN/OUT: Starting byte of reading
NAME=name, $ ; IN/OUT: Name to use for data
KNOWN_FILE=knownFile, $ ; IN: True if from example/data directory
GROUP=wGroup, $ ; IN: Parent for dialog
DATA=data, $ ; OUT: Data to read into.
TYPE=type, $ ; OUT: Type of data to read.
CLASSIFICATION=classification, $; IN/OUT: Classification of data.
FIRST_DATA_ITEM=firstDataItem, $; IN/OUT: First data item to read
LAST_DATA_ITEM=lastDataItem, $ ; IN/OUT: through last item to read
IMAGE=image ; OUT: True if an image
; Type of data
;
typeNames = ['Byte', 'Integer', 'Long Integer', 'Float', $
'Double Float', 'Complex', 'Double Complex']
dataTypes = [1,2,3,4,5,6,9] ; Corresponding SIZE() type values
dataTypeIndex = 0 ; default to Byte (index 0).
; Classification
;
classNames = ['Scalar', 'Vector', '2D Array', '3D Array', $
'4D Array', '5D Array', '6D Array', '7D Array', '8D Array', $
'Indexed Image', 'Pixel Interleaved Image', $
'Line Interleaved Image', 'Image Interleaved Image']
numDimensions = [0,1,2,3,4,5,6,7,8,2,3,3,3]
; Initialize values if they weren't passed in.
;
if (N_ELEMENTS(offset) eq 0) then $
offset = 0
if (N_ELEMENTS(firstDataItem) eq 0) then $
firstDataItem = 0
if (N_ELEMENTS(lastDataItem) eq 0) then $
lastDataItem = 0
if (N_ELEMENTS(description) eq 0) then $
description = ''
nDims = N_ELEMENTS(dimensions)
if (nDims eq 0) then $
dimensions = LONARR(8) $
else begin
dimensions = [dimensions, LONARR(8-nDims)]
endelse
if (N_ELEMENTS(classification) ne 0) then $
classTypeIndex = (WHERE(classNames eq classification, classCount))[0] $
else begin
dimIndexes = WHERE(dimensions gt 0, nDims)
if ((nDims eq 2) or (nDims eq 0)) then $
classTypeIndex = 9 $ ; Default 0D and 2D to type Image (index 9).
else $
classTypeIndex = nDims
classification = classNames[classTypeIndex]
endelse
; Create main base (non-sizable).
;
wMainBase = WIDGET_BASE(/COLUMN, TITLE='Structured Binary File', /MODAL, $
GROUP_LEADER=wGroup)
wTopBase = WIDGET_BASE(wMainBase, /COLUMN)
; Define base for format information.
;
wControlBase = WIDGET_BASE(wMainBase, /COLUMN, /FRAME, /BASE_ALIGN_LEFT)
; Define base for action buttons.
;
wActionBase = WIDGET_BASE(wMainBase, /ROW, /ALIGN_CENTER)
; ----------------------
; Input Parameters
; ----------------------
; Name of data
;
wRowBase = WIDGET_BASE(wTopBase, /ROW, XPAD=0, YPAD=0, $
/BASE_ALIGN_CENTER)
void = WIDGET_LABEL(wRowBase, VALUE='Data Name:', /ALIGN_LEFT)
wNameText = WIDGET_TEXT(wRowBase, VALUE=name, /EDITABLE, XSIZE=40)
; Description of data
;
wRowBase = WIDGET_BASE(wTopBase, /ROW, XPAD=0, YPAD=0, $
/BASE_ALIGN_CENTER)
void = WIDGET_LABEL(wRowBase, VALUE='Description:', /ALIGN_LEFT)
wDescriptionText = WIDGET_TEXT(wRowBase, VALUE=description, $
/EDITABLE, XSIZE=40)
; Informative description of steps to follow
;
void = WIDGET_LABEL(wControlBase, $
VALUE="Please set the following fields in the indicated order.", $
/ALIGN_LEFT)
; Offset into the file
;
wOffsetBase = WIDGET_BASE(wControlBase, /ROW, XPAD=0, YPAD=0, $
/BASE_ALIGN_CENTER)
wOffsetLabel = WIDGET_LABEL(wOffsetBase, $
VALUE="Step 1: Enter Starting Byte Offset:", $
/ALIGN_LEFT)
wOffsetText = WIDGET_TEXT(wOffsetBase, /EDITABLE, XSIZE=5, $
VALUE=STRING(offset, FORMAT='(I0)'), /ALL_EVENTS)
; IDL data type
;
wTypeList = WIDGET_DROPLIST(wControlBase, VALUE=typeNames, $
TITLE='Step 2: Select Data Type:')
WIDGET_CONTROL, wTypeList, SET_DROPLIST_SELECT=dataTypeIndex
; Classification
;
wClassList = WIDGET_DROPLIST(wControlBase, VALUE=classNames, $
TITLE='Step 3: Select Classification:')
WIDGET_CONTROL, wClassList, SET_DROPLIST_SELECT=classTypeIndex
; Dimensions of data
;
wDimLabel = WIDGET_LABEL(wControlBase, $
VALUE="Step 4: Enter Dimensions:", /ALIGN_LEFT)
wDimBase = WIDGET_BASE(wControlBase, /ROW, SPACE=0, XPAD=0, YPAD=0)
wSizeText = LONARR(8)
for seqDim = 0, 7 do begin
if (dimensions[seqDim] eq 0) then $
dimString = '' $
else $
dimString = STRING(dimensions[seqDim], FORMAT='(I0)')
wSizeText[seqDim] = WIDGET_TEXT(wDimBase, XSIZE=7, /EDITABLE, $
VALUE=dimString, /ALL_EVENTS)
endfor
; First through last data item to read
;
wElementBase = WIDGET_BASE(wControlBase, /ROW, XPAD=0, YPAD=0, $
/BASE_ALIGN_CENTER)
void = WIDGET_LABEL(wElementBase, VALUE='Step 5: From:', /ALIGN_LEFT)
wFirstDataItemText = WIDGET_TEXT(wElementBase, /EDITABLE, XSIZE=5, $
VALUE=STRING(firstDataItem, FORMAT='(I0)'), /ALL_EVENTS)
void = WIDGET_LABEL(wElementBase, VALUE='To:', /ALIGN_LEFT)
wLastDataItemText = WIDGET_TEXT(wElementBase, /EDITABLE, XSIZE=5, $
VALUE=STRING(lastDataItem, FORMAT='(I0)'), /ALL_EVENTS)
wClassLabel = WIDGET_LABEL(wElementBase, VALUE='('+classification+'s)', $
/DYNAMIC_RESIZE, /ALIGN_LEFT)
; A notice to specify how many bytes are left over
;
wLeftOverLabel = WIDGET_LABEL(wControlBase, /ALIGN_LEFT, VALUE='', $
/DYNAMIC_RESIZE)
; ----------------------
; Action Buttons
; ----------------------
padding = ' '
wOKButton = WIDGET_BUTTON(wActionBase, VALUE=' OK ')
void = WIDGET_LABEL(wActionBase, VALUE=padding)
void = WIDGET_LABEL(wActionBase, VALUE=padding)
wCancelButton = WIDGET_BUTTON(wActionBase, VALUE=' Cancel ')
psState = PTR_NEW ({ $
update: 0, $
offset: offset, $
dimensions: dimensions, $
description: description, $
name: name, $
dataTypeIndex: dataTypeIndex, $
fileSize: fileSize, $
dataTypes: dataTypes, $
numDimensions: numDimensions, $
firstDataItem:firstDataItem, $
lastDataItem: lastDataItem, $
classIndex: classTypeIndex, $
classNames: classNames, $
pData: PTR_NEW(), $
knownFile: knownFile, $
wMainBase: wMainBase, $
wClassList: wClassList, $
wFirstDataItemText: wFirstDataItemText, $
wLastDataItemText: wLastDataItemText, $
wTypeList: wTypeList, $
wSizeText: wSizeText, $
wNameText: wNameText,$
wDescriptionText: wDescriptionText,$
wOffsetText: wOffsetText,$
wClassLabel: wClassLabel, $
wLeftOverLabel: wLeftOverLabel, $
wOKButton: wOKButton, $
wCancelButton: wCancelButton $
})
if (knownFile) then begin
WIDGET_CONTROL, wOffsetLabel, SENSITIVE=0
WIDGET_CONTROL, wOffsetText, SENSITIVE=0
WIDGET_CONTROL, wTypeList, SENSITIVE=0
WIDGET_CONTROL, wClassList, SENSITIVE=0
WIDGET_CONTROL, wDimLabel, SENSITIVE=0
for seqDim = 0, 7 do $
WIDGET_CONTROL, wSizeText[seqDim], SENSITIVE=0
endif else $
dat_ComputeFields, psState
WIDGET_CONTROL, wMainBase, SET_UVALUE=psState
WIDGET_CONTROL, wMainBase, /REALIZE
; Start event loop.
;
WIDGET_CONTROL, wMainBase, DEFAULT_BUTTON=wOKButton, $
CANCEL_BUTTON=wCancelButton
XMANAGER, 'dat_PromptUser', wMainBase, EVENT_HANDLER='dat_HandleEvents'
update = (*psState).update
; If the user chose the OK button and no errors were found, copy the
; field values into the output keywords.
;
if (update) then begin
description = (*psState).description
data = (*(*psState).pData)
dimensions = (*psState).dimensions
offset = (*psState).offset
name = (*psState).name
description = (*psState).description
firstDataItem = (*psState).firstDataItem
lastDataItem = (*psState).lastDataItem
classification = classNames[(*psState).classIndex]
image = (classification eq 'Indexed Image' or $
classification eq 'Pixel Interleaved Image' or $
classification eq 'Line Interleaved Image' or $
classification eq 'Image Interleaved Image')
type = dataTypes[(*psState).dataTypeIndex]
endif
PTR_FREE, (*psState).pData
PTR_FREE, psState
RETURN, update
end
; -----------------------------------------------------------------------------
;
; Purpose: File PlugIn Read Routine.
;
pro ReadMydat, $
filename, $ ; IN: name of file to read
dataName, $ ; IN: default data name
TAIL=fileNameTail, $ ; IN: filename minus the directory name
GROUP=wGroup, $ ; IN: parent base for any dialogs created
_EXTRA=extra ; IN: information to pass to INSPUT
; Put up wait cursor.
;
WIDGET_CONTROL, /HOURGLASS
; If the file is in the example/data directory get its dimensions and
; description.
;
exampleFile = dat_CheckDataDir(filename, fileNameTail, $
DESCRIPTION=description, $
DIMENSIONS=dimensions, CLASSIFICATION=classification, $
FIRST_DATA_ITEM=firstDataItem, LAST_DATA_ITEM=lastDataItem)
; Open the file.
;
OPENR, unit, filename, /GET_LUN, /BLOCK, ERROR=ierror
if (ierror ne 0) then $
MESSAGE, 'Error occured opening file "' + filename + $
'".', /NONAME
; Get the file info to determine its size.
;
sFstat = FSTAT(unit)
; Ask the user (via a dialog) for more information.
;
if (dat_PromptUser(fileName, sFstat.size, DESCRIPTION=description, $
DIMENSIONS=dimensions, OFFSET=offset, NAME=dataName, DATA=data, $
FIRST_DATA_ITEM=firstDataItem, LAST_DATA_ITEM=lastDataItem, $
GROUP=wGroup, CLASSIFICATION=classification, TYPE=type, $
KNOWN_FILE=exampleFile, IMAGE=isImage)) then begin
nSkippedBytes = dat_SizeOf(type) * N_ELEMENTS(data) * firstDataItem
; Skip ahead in the file to the desired offset.
;
POINT_LUN, unit, offset+nSkippedBytes
; For each data item requested, read the file.
;
for dIndex = firstDataItem, lastDataItem do begin
READU, unit, data
sDataElement = CREATE_STRUCT(STRING(dIndex), data)
; Skip if not yet the first data item.
;
if (dIndex eq firstDataItem) then $
sData = sDataElement $
else $
sData = CREATE_STRUCT(sData, sDataElement)
endfor
if (N_ELEMENTS(sData) ne 0) then $
; Put all the data into the Data Manager.
;
INSPUT, $
sData, $ ; the data to put
NAME=dataName, $ ; the name to give it
DESCRIPTION=description, $ ; the data description
IMAGE=isImage, $ ; Set if image class
GROUP=wGroup, $ ; parent for dialogs
REPLACE=2, $ ; 2 = prompt if data name
; not unique.
_EXTRA=extra ; extra information
endif
; Close the file and free the unit
;
FREE_LUN, unit
end
; -----------------------------------------------------------------------------
;
; Purpose: File PlugIn Write Routine.
;
pro WriteMydat, $
filename, $ ; IN: name of file to write to
dataName, $ ; IN: name of data item to write, e.g., mydata
TAIL=tail, $ ; IN: (opt) filename w/out directory, e.g., myfile.dat
GROUP=wGroup, $ ; IN: (opt) ID of widget group leader
_EXTRA=extra ; IN: information to pass to INSGET
; Get the data from the Data Manager.
;
data = INSGET( $
dataName, $ ; data item to write
GROUP = wGroup, $ ; widget group leader
_EXTRA = extra) ; extra information
; Open the file.
;
OPENW, unit, filename, /GET_LUN, /BLOCK, ERROR=ierror
if (ierror ne 0) then $
MESSAGE, 'Error occured opening file "' + filename + $
'".', /NONAME
WRITEU, unit, data
; Free the unit
;
FREE_LUN, unit
end
; *****************************************************************************
; REGISTRATION FUNCTION
; *****************************************************************************
; -----------------------------------------------------------------------------
;
; Purpose: Register the file PlugIn.
;
function Mydat
; Return the File PlugIn Registration Structure.
;
RETURN, { $
type: 'File_PlugIn', $ ; PlugIn type
title: 'Structured Binary', $ ; PlugIn title
purpose: 'Handle .dat files.', $ ; PlugIn purpose
read_proc: 'ReadMydat', $ ; read callback
write_proc: 'WriteMydat', $ ; write callback
file_ext: 'dat', $ ; extension(s) (no period)
version: '5.0', $ ; IDL version
revision: '1.0' $ ; PlugIn version
}
end
; -----------------------------------------------------------------------------